package 集合;
import java.util.function.*;
import java.util.*;
/*
  Consumer<T>接口 消费者模式接口 
  Function<T,R>接口 T 类型  R 返回类型，表示接收一个参数并返回结果的函数
  Supplier<T>接口  代表结果供应商
  Predicate<T>  断言
  
*/
public class X05Jdk8新接口
{
	public static void main(String[] args){
		//Function接口示例
		//strtoup("ajshd",(s)->s.toUpperCast);//等同于下面
		String s=strtoUp("nihaodd", new Function<String,String>(){

				@Override
				public String apply(String str)
				{
					// TODO: Implement this method
					return str.toUpperCase();
				}			
			});
		System.out.println(s);
		//Supplier接口示例
		//getNums(15,()->(int)Math.random()*100);
		ArrayList<Integer> list=getNums(15, new Supplier<Integer>(){

				@Override
				public Integer get()
				{
					// TODO: Implement this method
					return (int)(Math.random()*100);
				}
			});
		Iterator it= list.iterator();
		while(it.hasNext()){
			System.out.println(it.next());
		}
		//list.forEach(System.out::println);
		//Predicate断言接口示例
		List<String> list2=Arrays.asList("djsjd","dufhs","mgjrjs","hchdhe","abcej","mvkfo","oqirjc");
		List<String> result=filter(list2, new Predicate<String>(){

				@Override
				public boolean test(String p1)
				{
					// TODO: Implement this method
					return p1.contains("o");
				}
			});
		for(String ss :result){
			System.out.println(ss);
		}
	}
	//Function接口
	private static String strtoUp(String str,Function<String,String> f){
		return f.apply(str);
	}
	//Supplier接口 实现获取几个随机整数
	private static ArrayList<Integer> getNums(int num,Supplier<Integer> sup){
		ArrayList<Integer> list=new ArrayList<>();
		for(int i= 0;i<num;i++){
			list.add(sup.get());
		}
		return list;
	}
	//Predicate断言接口
	private static List<String> filter(List<String> list,Predicate<String> pre){
		List<String> result=new ArrayList<>();
		for(String str:list){
			if(pre.test(str)){
				result.add(str);
			}
		}
		return result;
	}
}
